package com.renjibo.teawithmilkgoods.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.renjibo.teawithmilkgoods.R;

/**
 * 任继波
 * Created by Administrator on 2017/2/15.
 */

public class MyTextViewFrame extends TextView {
    /**
     * 四周是否带有边框【true:四周带有边框】【false:四周不带边框】
     */
    boolean borders = true;
    /**
     * 左边是否带有边框【true:左侧带有边框】【false:左侧不带边框】
     */
    boolean borderLeft = true;
    /**
     * 顶部是否带有边框【true:顶部带有边框】【false:底部不带边框】
     */
    boolean borderTop = true;
    /**
     * 右侧是否带有边框【true:右侧带有边框】【false:右侧不带边框】
     */
    boolean borderRight = false;
    /**
     * 底部是否带有边框【true:底部带有边框】【false:底部不带边框】
     */
    boolean borderBottom = false;
    /**
     * 边框颜色
     */
    String textColor = "#ff000000";
    public MyTextViewFrame(Context context) {
        this(context,null);
    }

    public MyTextViewFrame(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyTextViewFrame(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义属性集
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.MyTextViewFrame);
        // 是否设置全部边框，默认为false
        borders = typedArray.getBoolean(
                R.styleable.MyTextViewFrame_layout_borders, true);
        // 是否设置左侧边框，默认为false
        borderLeft = typedArray.getBoolean(
                R.styleable.MyTextViewFrame_layout_borderLeft, false);
        // 是否设置顶部边框，默认为false
        borderTop = typedArray.getBoolean(
                R.styleable.MyTextViewFrame_layout_borderTop, false);
        // 是否设置右侧边框，默认为false
        borderRight = typedArray.getBoolean(
                R.styleable.MyTextViewFrame_layout_borderRight, false);
        // 是否设置底部边框，默认为false
        borderBottom = typedArray.getBoolean(
                R.styleable.MyTextViewFrame_layout_borderBottom, false);
        // 获取文本颜色值，用来画边框的，便于和文本颜色匹配
        textColor = attrs.getAttributeValue(
                "http://schemas.android.com/apk/res/android", "textColor");
        typedArray.recycle();
    }
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // 创建画笔
        Paint paint = new Paint();
        // 获取该画笔颜色
        int color = paint.getColor();
        // 设置画笔颜色
        paint.setColor(Color.parseColor(textColor));
        // 如果borders为true，表示左上右下都有边框
        if (borders) {
            canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
            canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
            canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
                    this.getHeight() - 1, paint);
            canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
                    this.getHeight() - 1, paint);
        } else {
            if (borderLeft) {
                // 画左边框线
                canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
            }
            if (borderTop) {
                // 画顶部边框线
                canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
            }
            if (borderRight) {
                // 画右侧边框线
                canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
                        this.getHeight() - 1, paint);
            }
            if (borderBottom) {
                // 画底部边框线
                canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
                        this.getHeight() - 1, paint);
            }
        }
        // 设置画笔颜色归位
        paint.setColor(color);
    }
}
